package org.acghub.mtdb.common.type;

import org.acghub.mtdb.common.Visitor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.function.Function;

/**
 * @author zhouhe
 */
public class TypeDateTime extends TypeBasePrimitives {
    public static final DateTimeFormatter DEFAULT_DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");

    public static String format(LocalDateTime value) {
        return DEFAULT_DATE_TIME_FORMAT.format(value);
    }

    public static LocalDateTime parse(String value) {
        return LocalDateTime.parse(value, DEFAULT_DATE_TIME_FORMAT);
    }

    public TypeDateTime() {
        super("datetime");
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }

    @Override
    protected Function<String, Object> checkDefaultValueFunc() {
        return TypeDateTime::parse;
    }

    /**
     * 获取java接口类型
     *
     * @return
     */
    @Override
    public String javaInterfaceType() {
        return "java.time.LocalDateTime";
    }

    @Override
    public String javaImplType() {
        return javaInterfaceType();
    }

    @Override
    public String getDefaultValue() {
        return "null";
    }

    @Override
    public boolean isConstType() {
        return true;
    }

    @Override
    public void supportDefaultValue(String value) {
        try {
            Object o = parse(value);
        } catch (Exception e) {
            throw new IllegalArgumentException(getName() + "类型默认值错误，default:" + value, e);
        }
    }
}
